import * as THREE from '../../build/three.module';
import * as d3 from 'd3-geo';
class Utils {
  /**
   * 地图数据解码
   */
  decode(json) {
    if (!json.UTF8Encoding) {
      return json;
    }
    let encodeScale = json.UTF8Scale;
    if (!encodeScale) {
      encodeScale = 1024;
    }
    let features = json.features;

    features.forEach(feature => {
      let geometry = feature.geometry;
      let coordinates = geometry.coordinates;
      let encodeOffsets = geometry.encodeOffsets;
      coordinates.forEach((coordinate, c) => {
        if (geometry.type === 'Polygon') {
          coordinates[c] = this.decodePolygon(coordinate, encodeOffsets[c], encodeScale);
        } else if (geometry.type === 'MultiPolygon') {
          coordinate.forEach((polygon, c2) => {
            coordinate[c2] = this.decodePolygon(polygon, encodeOffsets[c][c2], encodeScale);
          });
        }
      });
    });
    // Has been decoded
    json.UTF8Encoding = false;
    return json;
  }

  /**
   * @desc 解码
   */
  decodePolygon(coordinate, encodeOffsets, encodeScale) {
    const result = [];
    let prevX = encodeOffsets[0];
    let prevY = encodeOffsets[1];

    for (let i = 0; i < coordinate.length; i += 2) {
      let x = coordinate.charCodeAt(i) - 64;
      let y = coordinate.charCodeAt(i + 1) - 64;
      // ZigZag decoding
      x = (x >> 1) ^ -(x & 1);
      y = (y >> 1) ^ -(y & 1);
      // Delta deocding
      x += prevX;
      y += prevY;

      prevX = x;
      prevY = y;
      // Dequantize
      result.push([x / encodeScale, y / encodeScale]);
    }
    return result;
  }

  /**
     * @description 地理坐标转换成屏幕坐标
     * @param {Array} coordinates_array
     * @param {Float32Array} sphere_radius
     */
    convertToSphereCoords(coordinates_array, sphere_radius) {
      const pts = [];
      coordinates_array.forEach(pt => {        
        let lon = pt[0];
        let lat = pt[1];

        let coord = [];
        coord.push(Math.cos(lat * Math.PI / 180) * Math.cos(lon * Math.PI / 180) * sphere_radius);
        coord.push(Math.cos(lat * Math.PI / 180) * Math.sin(lon * Math.PI / 180) * sphere_radius);
        coord.push(Math.sin(lat * Math.PI / 180) * sphere_radius);
        pts.push(new THREE.Vector3(coord[2],coord[1],coord[0]));
      });
      return pts;
  }

  lglt2xyz (lg, lt, r) {
    lg = lg * Math.PI / 180
    lt = lt * Math.PI / 180
    return new THREE.Vector3(
        Math.cos(lg) * Math.cos(lt) * r,
        Math.sin(lg) * Math.cos(lt) * r,
        Math.sin(lt) * r
    );
}

/**
   * @desc 经纬度转换成墨卡托投影
   * @param {array} 传入经纬度
   * @return array [x,y,z]
   */
  lnglatToMector(lnglat) {
    if (!this.projection) {
      this.projection = d3
        .geoMercator()
        .center([108.904496, 32.668849])
        .scale(80)
        .rotate(Math.PI / 4)
        .translate([0, 0]);
    }
    const [y, x] = this.projection([...lnglat]);
    let z = 0;
    return [x, y, z];
  }

  convertSphereCoordsToVertext(input){
    let output = [];
    if(input.length){
      input.forEach(el =>{
        output.push(new THREE.Vector3(el[0],el[1],el[2]));
      });
    } 
    return output;
  }

  
}

export const util = new Utils();
